package reName;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Demo {
	public static void main(String[] args) {
		 try {
			test1();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test1() {
		long begin = System.currentTimeMillis();
		File rootFile = new File("C:\\t");
		File[] filesList = rootFile.listFiles();
		long len = filesList.length;
		for (int id = 0; id < len; id++) {
			File oldFile = filesList[id];
			String parentPath = oldFile.getParent();
			parentPath = parentPath.replaceAll("\\\\", "/");
			if (!parentPath.endsWith("/")) {
				parentPath += "/";
			}
			if (oldFile.isFile()) {
				String fileName = oldFile.getName();
				String prefix = fileName.substring(fileName.lastIndexOf("."));
				File newFile = new File(parentPath + id + prefix);
				oldFile.renameTo(newFile);
				// System.out.println(prefix);
			}
		}
		long end = System.currentTimeMillis();
		System.out.println((end - begin) + "毫秒");

	}

	public static void test2() throws InterruptedException {
		long begin = System.currentTimeMillis();
		File rootFile = new File("C:\\t");
		File[] filesList = rootFile.listFiles();
		long len = filesList.length;
		ExecutorService executorService = Executors.newFixedThreadPool(8);
		for (int id = 0; id < len; id++) {
			executorService.execute(new RenameDemo("good", id, filesList[id]));
		}
		executorService.shutdown();
		executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);// 所有线程执行完毕才进入下一步
		long end = System.currentTimeMillis();
		System.out.println((end - begin) + "毫秒");

	}
}
